#!/usr/bin/env python
#Author Luciano Fraga
#2011/04/06

from pyraf import iraf
from pyraf.iraf import pysoar
from pyraf.irafglobals import yes,no

from soarkey import SoarKey
import soarprint 

def FixHeader(image,verbose):
        '''This function has the values to be fixed on the header and apply the changes.
           This function will fix images with the 2x2 and 4x4 binning, the 1x1 should be OK.
        '''
        
        Header2x2 = {} #TODO: This should be a text file to include new values if needed.
        Header2x2['OBSERVAT'] = ['SOAR','SOAR','SOAR','SOAR']
        Header2x2['DATASEC'] = ['[28:539,1:2048]' ,'[29:540,1:2048]','[28:539,1:2048]','[29:540,1:2048]']
        Header2x2['BIASSEC'] = ['[543:562,1:2048]','[1:20,1:2048]'  ,'[543:562,1:2048]','[1:20,1:2048]']       
        Header2x2['TRIMSEC'] = ['[28:539,1:2048]' ,'[29:540,1:2048]','[28:539,1:2048]','[29:540,1:2048]']       
        Header2x2['CTYPE1'] = ['RA---TAN','RA---TAN','RA---TAN','RA---TAN']       
        Header2x2['CTYPE2'] = ['DEC--TAN','DEC--TAN','DEC--TAN','DEC--TAN']       
        Header2x2['CD1_1'] = ['-4.265e-05','-4.265e-05','-4.265e-05','-4.265e-05']       
        Header2x2['CD2_2'] = ['4.265e-05','4.265e-05','4.265e-05','4.265e-05']       
        Header2x2['CD1_2'] = ['1.42e-07','1.42e-07','1.42e-07','1.42e-07']
        Header2x2['CD2_1'] = ['1.42e-07','1.42e-07','1.42e-07','1.42e-07']       
        Header2x2['SATURATE'] = ['65000.','65000.','65000.','65000.']       
        Header2x2['EQUINOX'] = ['2000.','2000.','2000.','2000.']       

        Header4x4 = {} #TODO: This should be a text file to include new values if needed. 
        Header4x4['OBSERVAT'] = ['SOAR','SOAR','SOAR','SOAR']
        Header4x4['DATASEC'] = ['[16:271,1:1024]','[13:268,1:1024]','[16:271,1:1024]','[13:268,1:1024]']
        Header4x4['BIASSEC'] = ['[274:282,1:1024]','[2:10,1:1024]','[274:282,1:1024]','[2:10,1:1024]']       
        Header4x4['TRIMSEC'] = ['[16:271,1:1024]','[13:268,1:1024]','[16:271,1:1024]','[13:268,1:1024]']       
        Header4x4['CTYPE1'] = ['RA---TAN','RA---TAN','RA---TAN','RA---TAN']       
        Header4x4['CTYPE2'] = ['DEC--TAN','DEC--TAN','DEC--TAN','DEC--TAN']       
        Header4x4['CD1_1'] = ['-4.265e-05','-4.265e-05','-4.265e-05','-4.265e-05']       
        Header4x4['CD2_2'] = ['4.265e-05','4.265e-05','4.265e-05','4.265e-05']       
        Header4x4['CD1_2'] = ['1.42e-07','1.42e-07','1.42e-07','1.42e-07']
        Header4x4['CD2_1'] = ['1.42e-07','1.42e-07','1.42e-07','1.42e-07']       
        Header4x4['SATURATE'] = ['65000.','65000.','65000.','65000.']       
        Header4x4['EQUINOX'] = ['2000.','2000.','2000.','2000.']       

        Header5x5 = {} #TODO: This should be a text file to include new values if needed. 
        Header5x5['OBSERVAT'] = ['SOAR','SOAR','SOAR','SOAR']
        Header5x5['DATASEC'] = ['[14:217,1:819]','[14:217,1:819]','[14:217,1:819]','[14:217,1:819]']
        Header5x5['BIASSEC'] = ['[220:229,1:819]','[2:10,1:819]','[220:229,1:819]','[2:10,1:819]']       
        Header5x5['TRIMSEC'] = ['[14:217,1:819]','[14:217,1:819]','[14:217,1:819]','[14:217,1:819]']
        Header5x5['CTYPE1'] = ['RA---TAN','RA---TAN','RA---TAN','RA---TAN']       
        Header5x5['CTYPE2'] = ['DEC--TAN','DEC--TAN','DEC--TAN','DEC--TAN']       
        Header5x5['CD1_1'] = ['-4.265e-05','-4.265e-05','-4.265e-05','-4.265e-05']       
        Header5x5['CD2_2'] = ['4.265e-05','4.265e-05','4.265e-05','4.265e-05']       
        Header5x5['CD1_2'] = ['1.42e-07','1.42e-07','1.42e-07','1.42e-07']
        Header5x5['CD2_1'] = ['1.42e-07','1.42e-07','1.42e-07','1.42e-07']       
        Header5x5['SATURATE'] = ['65000.','65000.','65000.','65000.']       
        Header5x5['EQUINOX'] = ['2000.','2000.','2000.','2000.']       

        if image.ccdbin == '2x2':
                Header = Header2x2
                to_be_fixed = True
                
        elif image.ccdbin == '4x4':
                Header = Header4x4
                to_be_fixed = True

        elif image.ccdbin == '5x5':
                Header = Header5x5
                to_be_fixed = True
        else:
                message = '-- SOIPREPARE: on image %s: this code has no implementation for this binning (ccdbin = %s).'\
                                                                                          %(img.filename,image.ccdbin)
                soarprint.warning(logfile,message)                
                to_be_fixed = False
        
        if to_be_fixed:
                for amp,imgext  in enumerate(image.imext):
                        for ikeyword in Header.keys():
                                iraf.hedit(imgext,fields=ikeyword,value=Header[ikeyword][amp],\
                                           update='yes',addonly='no',delete='no',verify= 'no', show=verbose)

def soiprepare(input,logfile,verbose):
        '''This is a temporary task to fix wrong values on SOIs header for CCDSEC, BIASSEC and TRIMSEC.
        '''
        
        infiles = iraf.files(input, Stdout=1)

        for ifile in infiles:
                try:
                        img = SoarKey(ifile,logfile,HDU=1)
                        if verbose == yes: 
                                message = 'WORKING ON IMAGE %s'%(img.filename)
                                soarprint.log(logfile,message,verbose)
                        img.getccdbin()
                        img.getextensions()
                        FixHeader(img,verbose)
                except:
                         message = '-- SOIPREPARE: image %s is a single fits file.'%(ifile)
                         soarprint.warning(logfile,message)                
                        
parfile = iraf.osfn("pysoar$soi/soiprepare.par")
t = iraf.IrafTaskFactory(taskname="soiprepare",value=parfile,function=soiprepare)
